package vanilla_fss;

/*
 * Copyright (c) 2011 Murilo Rebelo Pontes
 * murilo.pontes@gmail.com
 * 
 * GNU LESSER GENERAL PUBLIC LICENSE (Version 2.1, February 1999)
 */

public class FSS_AdaptiveStepSize {
	FSS_PRNG r = null;
	
	int success_now = 0;
	int success_past = 0;
	double adapt_direction = 1;

	double stepsize = Double.NaN;
	
	FSS_AdaptiveStepSize(FSS_PRNG r){
		this.r = r;
		init();
	}
	private void init(){
		stepsize = r.between(FSS_Parameters.stepsize_min_init, FSS_Parameters.stepsize_max_init);
	}
	public void adapt(){
		//if get greater success keep adapt direction
		if(success_now> success_past){
			adapt_direction = 1;
		}
		//otherwise invert adapt direction 
		else {
			adapt_direction = -1;
		}
		//adapt step size 
		stepsize += adapt_direction*FSS_Parameters.adapt_rate;
		//take care about min and max step size 
		if (stepsize<FSS_Parameters.stepsize_min) stepsize=FSS_Parameters.stepsize_min;
		if (stepsize>FSS_Parameters.stepsize_max) stepsize=FSS_Parameters.stepsize_max;
	}
}
